package com.software.adapterview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ArrayAdapterActivity extends AppCompatActivity {

    private ListView lv_course;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_array_adapter);
        //数据源
        List<String> courseList = new ArrayList<>();
        courseList.add("Android");
        courseList.add("Java");
        courseList.add("C++");
        courseList.add("Python");
        //适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_course, courseList);


        initViews();
        //AdapterView和适配器绑定
        lv_course.setAdapter(adapter);

        lv_course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ArrayAdapterActivity.this, courseList.get(position), Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initViews() {
        lv_course = findViewById(R.id.lv_course);

    }
}